package com.naiterui.ehp.bs.emr.service.impl;

import com.itextpdf.text.DocumentException;
import com.naiterui.ehp.bp.utils.lang.ConfigUtil;
import com.naiterui.ehp.bs.emr.entity.mongo.MedicalRecord;
import com.naiterui.ehp.bs.emr.entity.mongo.MedicalRecordDoctor;
import com.naiterui.ehp.bs.emr.repository.mongo.MedicalRecordRepository;
import com.naiterui.ehp.bs.emr.service.IEmrPdfService;
import com.naiterui.ehp.bs.emr.utils.EmrPdfUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author guoyongxiang
 * @date 2019/9/10 20:04
 * @since 1.0.0
 */
@Service
public class EmrPdfServiceImpl implements IEmrPdfService {

    private static final Logger LOGGER = LoggerFactory.getLogger(EmrPdfServiceImpl.class);

    @Autowired
    private MedicalRecordRepository medicalRecordRepository;
    @Autowired
    private EmrPdfUtil emrPdfUtil;

    //    @Value("${file.base.prefix}")
    private String emrBasePath;

    @Override
    public String generatePdf(String emrId) throws IOException, DocumentException {
        MedicalRecord medicalRecord = medicalRecordRepository.findByIdAndCaseTypeAndAvailableNot(emrId, 4, 1);
        List<String> imgFiles =
                Optional.ofNullable(medicalRecord)
                        .map(MedicalRecord::getRecordDoctor)
                        .map(MedicalRecordDoctor::getImgList)
                        .map(l -> l.stream().map(img -> emrBasePath + img.getUrl()).collect(Collectors.toList()))
                        .orElse(null);

        EmrPdfUtil.EmrBO emrBO = EmrPdfUtil.EmrBO.builder()
                                                 .emrId(medicalRecord.getId())
                                                 .patientId(medicalRecord.getPatientId())
                                                 .name(medicalRecord.getName())
                                                 .age(medicalRecord.getAgeStr())
                                                 .ageUnit(medicalRecord.getAgeUnit())
                                                 .departmentName(medicalRecord.getDepartment())
                                                 .createdAt(medicalRecord.getCreateAt())
                                                 .gender(medicalRecord.getGender())
                                                 .chiefComplaint(medicalRecord.getRecordDoctor().getMainComplaint())
                                                 .pastMedicalHistory(medicalRecord.getRecordDoctor().getPastHistory())
                                                 .temperature(medicalRecord.getRecordDoctor().getTemperature())
                                                 .weight(medicalRecord.getRecordDoctor().getWeight())
                                                 .heartRate(medicalRecord.getRecordDoctor().getHeartRete())
                                                 .systolicPressure(medicalRecord.getRecordDoctor().getSystolic())
                                                 .diastolicPressure(medicalRecord.getRecordDoctor().getDiastole())
                                                 .diagnosis(medicalRecord.getRecordDoctor().getDiagnosisList())
                                                 .doctorName(medicalRecord.getDoctorName())
                                                 .hospitalName(ConfigUtil.getString("ehp.hospital.name"))
                                                 .reportImgUrls(imgFiles)
                                                 .build();
        LOGGER.info("病例：{}", medicalRecord);
        return emrPdfUtil.generatePDF(emrBO);
    }

}
